codewriting
You're trying to solve a puzzle that involves arranging squares of numbers according to their missing values. Each square has dimensions 4 × 4, containing all the numbers between 1 and 16 inclusively, except for one missing number represented by "?". All of these 4 × 4 squares are stored side-by-side within a larger matrix mat with dimensions 4 × (4 * n) (where n represents the number of square matrices).
Your task is to complete the following steps:
- For each
4 × 4square, find the value of the missing element and replace the"?"with this value. - Rearrange the squares inside the larger matrix by these missing values in ascending order. In the case of a tie (if two
4 × 4matrices have the same missing value), place them in the relative order they were originally presented in the larger matrixmat.
Return the updated matrix mat as a result.
Example
- For
mat = [["1", "2", "3", "4"],
["?", "5", "6", "10"],
["13", "16", "12", "15"],
["9", "7", "8", "14"]]
the output should be
sortByMissingNum(mat) = [["1", "2", "3", "4"],
["11", "5", "6", "10"],
["13", "16", "12", "15"],
["9", "7", "8", "14"]]
The given matrix mat contains only one 4 × 4 square, so we just find the missing value in it (11), and replace the "?" with this value.
- For
mat = [["14", "3", "10", "4", "16", "10", "?", "2", "?", "9", "15", "11"],
["16", "7", "8", "2", "1", "4", "8", "3", "3", "16", "7", "13"],
["?", "9", "6", "5", "14", "12", "7", "6", "2", "10", "4", "14"],
["15", "1", "13", "12", "9", "15", "5", "13", "1", "8", "12", "6"]]
the output should be
sortByMissingNum(mat) = [["5", "9", "15", "11", "14", "3", "10", "4", "16", "10", "11", "2"],
["3", "16", "7", "13", "16", "7", "8", "2", "1", "4", "8", "3"],
["2", "10", "4", "14", "11", "9", "6", "5", "14", "12", "7", "6"],
["1", "8", "12", "6", "15", "1", "13", "12", "9", "15", "5", "13"]]

